package com.tzy.powergrid.controller;
import com.tzy.powergrid.utils.Doc2HtmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
 * java页面展示pdf文件 (Word转PDF)
 */
@Slf4j
@Controller
@RequestMapping("/inspection/report")
public class InspectionReportController {
    @GetMapping("/show")
    public String show(){
        return "show";
    }

    //这个方法是转换的方法
    @RequestMapping(value = "/converter", method = RequestMethod.POST)
    @ResponseBody
//    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
    public String readFile() throws IOException {
        File file = new File("E:\\Download\\_y2项目\\1谭-Qixin\\Power Grid报告相关\\新电压监测仪检测报告.doc");
        log.info("fileName={},exists={}",file.getName(),file.exists());
        if (!file.exists()) {
            try {
                // 这里只是简单例子，文件直接输出到项目路径下。
                // 实际项目中，文件需要输出到指定位置，需要在增加代码处理。


                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream("e:/"+file.getName()));
                System.out.println(file.getName());
                out.write((int) file.length());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "转换失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "转换失败," + e.getMessage();
            }

            //调用Doc2HtmlUtil工具类
            Doc2HtmlUtil coc2HtmlUtil = Doc2HtmlUtil.getDoc2HtmlUtilInstance();
            File file1 = null;
            FileInputStream fileInputStream = null;
            //这里写的是被转文件的路径
            file1 = new File("e:/"+file.getName());

            fileInputStream = new FileInputStream(file1);

//为了后期方便，这里直接把文件名进行了截取，方便后续操作
            int i = file.getName().lastIndexOf(".");
            String substring = file.getName().substring(i);
            //上述的所有路径以及以下路劲均可自定义
            coc2HtmlUtil.file2pdf(fileInputStream, "e:/",substring);
            Doc2HtmlUtil doc = new Doc2HtmlUtil();


            return "转换成功";


        } else {
            return "转换失败，因为文件是空的.";
        }
    }




    @GetMapping("/show/pdf")
    public void findPdf(HttpServletRequest request,HttpServletResponse response) throws IOException {
//        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("application/pdf");
        FileInputStream in =
                new FileInputStream("E:\\学习\\Y2项目\\1谭'SinoQixin\\Power Grid\\关于加强近期网络安全管控的涵.pdf");
        OutputStream out = response.getOutputStream();
        byte[] b = new byte[512];
        while ((in.read(b))!=-1) {
            out.write(b);
        }
        out.flush();
        in.close();
        out.close();
//        return "showPdf";
    }

    /**
     *  function finPdf(){
     * window.open(url, "_blank","top=200,left=200,height=600,width=800,status=yes,toolbar=1,
     * menubar=no,location=no,scrollbars=yes");
     * }
     */



}
